package com.tcwlkj.uuService.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.adapter.MessageAdapter;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.MessageModel;
import com.tcwlkj.uuService.utils.MyRefresh;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/9/30 0030.
 */

public class MessageNotificationActivity extends BaseActivity{
    @Bind(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.lay_total_empty)
    LinearLayout lay_total_empty;

    private ArrayList<MessageModel> modelList = new ArrayList<MessageModel>();
    private MessageAdapter messageAdapter=new MessageAdapter(modelList);
    private int pindex=1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        initview();
    }
    private void initview() {
        changeTitle("消息通知");
        final LinearLayoutManager layoutManager = new LinearLayoutManager(baseContext);
        recyclerView.setLayoutManager(layoutManager);
        MyRefresh.getInstance().getMusic(refreshLayout,false);
        recyclerView.setAdapter(messageAdapter);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                modelList.clear();
                pindex = 1;
                getuselist(pindex);
                refreshLayout.finishRefreshing();
            }
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                pindex++;
                getuselist(pindex);
                refreshLayout.finishLoadmore();
            }
        });
        messageAdapter.setOnItemClickListener(new MessageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent();
                intent.setClass(MessageNotificationActivity.this,MessageDetailActivity.class);
                intent.putExtra("noticeid",modelList.get(position).getID());
                startActivity(intent);
            }
        });
        getuselist(pindex);
    }

    public void getuselist(int pindex) {
        Call<CommonResponse<ArrayList<MessageModel>>> httpResultCall = RetrofitUtils.getRequest(this, false).uselist(PreferencesUtils.getString(MyApp.getContext(), "userID"),pindex,10);
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<ArrayList<MessageModel>>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<ArrayList<MessageModel>> commonResponse= (CommonResponse<ArrayList<MessageModel>>) t;
                if (commonResponse.getTotal().equals("0")) {
                    lay_total_empty.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    return;
                }
                if(modelList.size()>=Integer.parseInt(commonResponse.getTotal())){
                    ToastUtil.getInstance().show("没有更多数据");
                    return;
                }
                modelList.addAll(commonResponse.getData());
                messageAdapter.updateData(modelList);
            }
            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    getuselist(pindex);
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
